<template>
	<div>
		<el-row :gutter="20">
			<el-col :xs="12" :sm="5" :md="5" :lg="4"><el-input placeholder="请输入名称" size="small" v-model="listQuery.name" clearable></el-input></el-col>
			<el-col :xs="12" :sm="8" :md="8" :lg="4"><el-input placeholder="请输入创建人" size="small" v-model="listQuery.createName" clearable></el-input></el-col>
			<el-col :xs="12" :sm="4" style="padding: 0">
				<el-button type="primary" size="small" icon="el-icon-search" @click="getList()" plain>搜索</el-button>
				<el-button type="primary" size="small" icon="el-icon-plus" @click="showEditDialog('add')">新增</el-button>
			</el-col>
		</el-row>

		<el-table :data="dictionaryTableData" style="width: 100%" stripe v-loading="loading">
			<el-table-column fixed prop="name" label="名称" show-overflow-tooltip=""></el-table-column>
			<el-table-column prop="createName" label="创建人" show-overflow-tooltip=""></el-table-column>
			<el-table-column prop="ipAddr" label="ip地址" width="150"></el-table-column>
			<el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
			<el-table-column label="操作" width="200">
				<template slot-scope="{ row }">
					<el-button-group>
						<el-button type="primary" size="mini" @click="resetBackupDb(row.id)" class="jurisdiction">恢复</el-button>
						<el-button type="primary" size="mini" @click="showEditDialog('edit', row)" class="jurisdiction">编辑</el-button>
					</el-button-group>
				</template>
			</el-table-column>
		</el-table>
		<Pagination
			style="margin-top: 20px"
			v-show="parseInt(pageObj.count) > 0"
			:total="parseInt(pageObj.count)"
			@change="getList"
			:page.sync="listQuery.page"
			:limit.sync="listQuery.limit"
		/>

		<el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="700px" class="dialog-wrap" @close="$reset('temp')" :close-on-click-modal="false">
			<el-form ref="temp" :model="temp" :rules="rules" label-width="80px">
				<el-form-item label="名称" prop="name">
					<el-input v-model="temp.name"></el-input>
				</el-form-item>
				<el-form-item label="描述" prop="content">
					<el-input type="textArea" v-model="temp.content"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button type="primary" v-preventReClick @click="handleAdd('temp')">保 存</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { getBlackupDblist, setBackupDb, editBackupDb, resetBackupDb } from '@/api/admin/system/backUp.js';
export default {
	created() {
		this.getList();
	},
	data() {
		return {
			temp: {},
			dialogTitle: '新增备份',
			listQuery: {
				page: 1,
				limit: 10,
				content: '', //描述
				name: '', //名称
				ipAddr: ''
			},
			pageObj: {},
			loading: false,
			addOrEdit: true,
			dialogFormVisible: false,
			rules: {
				name: [
					{ required: true, message: '请输入名称', trigger: 'blur' },
					{
						trigger: 'blur'
					}
				],
				content: [
					{ required: true, message: '请输入描述', trigger: 'blur' },
					{
						trigger: 'blur'
					}
				]
			},
			dictionaryTableData: []
		};
	},

	methods: {
		//获取列表
		getList() {
			this.loading = true;
			getBlackupDblist(this.listQuery).then((res) => {
				this.dictionaryTableData = res.data.data;
				this.pageObj = res.data;
				this.loading = false;
			});
		},
		// 显示编辑弹出框
		showEditDialog(type, row) {
			if (type == 'edit') {
				this.addOrEdit = true;
				this.dialogTitle = '新增备份';
			} else {
				this.addOrEdit = false;
				this.dialogTitle = '编辑备份';
			}
			this.temp = { ...row };

			this.dialogFormVisible = true;
		},
		//编辑
		handleUpdate(temp) {},
		resetBackupDb(id) {
			this.$confirm('此操作系统将恢复到之前保存的状态, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(async () => {
					resetBackupDb(id).then((res) => {
						if (res.data.code == 0) {
							this.$message.success('成功');
						} else {
							this.$message.error('失败');
						}
					});
				})
				.catch(() => {
					this.$message({
						type: 'info',
						message: '已取消'
					});
				});
		},
		handleAdd(temp) {
			this.$refs[temp].validate((valid) => {
				if (valid) {
					if (this.addOrEdit) {
						//修改
						this.temp.dictId = this.listQuery.dictId;
						editBackupDb(this.temp).then((res) => {
							if (res.data.code == 0) {
								this.$message.success('保存成功');
								this.dialogFormVisible = false;
								this.getList();
							} else {
								this.$message.error(res.data.msg);
							}
						});
					} else {
						//新增
						setBackupDb(this.temp).then((res) => {
							if (res.data.code == 0) {
								this.$message.success('保存成功');
								this.dialogFormVisible = false;

								this.getList();
							} else {
								this.$message.error(res.data.msg);
							}
						});
					}
				}
			});
		},
		// 删除
		showDeleDialog(row) {}
	}
};
</script>
<style>
.dialog-wrap .el-form-item__label:before {
	content: '*';
	color: #f56c6c;
	margin-right: 4px;
}
</style>
